package views;

import views.panels.PanelGeneral;
import logicBusiness.EstudianteBL;
import logicBusiness.GradoBL;
import models.Estudiante;
import java.awt.BorderLayout;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author sebastián
 */
public class InterfazInscripcion extends javax.swing.JFrame {

    private EstudianteBL estudiantebo;
    private GradoBL gradobo;

    /**
     * Esta funcion inicializa la tabla en la que
     * se muestran los estudiantes preinscritos
     */
    @SuppressWarnings("empty-statement")
    private void inicializarTablaDePreinscritos(List<Estudiante> listaentrada) {
        int numfilas = listaentrada.size();

        TInscripcion.setModel(new DefaultTableModelImpl(
                new Object[numfilas][5],
                new String[]{
                    "Documento", "Apellido", "Nombre", "Fecha de inscripcion", "Grado al que aspira", "Inscrito"
                }));


        for (int indicefilas = 0; indicefilas < numfilas; indicefilas++) {
//            this.TInscripcion.setValueAt(listaentrada.get(indicefilas).getDocEstudiante(), indicefilas, 0);
//            this.TInscripcion.setValueAt(listaentrada.get(indicefilas).getApellidoEst(), indicefilas, 1);
//            this.TInscripcion.setValueAt(listaentrada.get(indicefilas).getNombreEst(), indicefilas, 2);
//            this.TInscripcion.setValueAt(listaentrada.get(indicefilas).getFechaPreinscripcion(), indicefilas, 3);
//            this.TInscripcion.setValueAt(listaentrada.get(indicefilas).getIdGrado(), indicefilas, 4);
//            if (listaentrada.get(indicefilas).getInscrito() == 0) {
//                this.TInscripcion.setValueAt("No", indicefilas, 5);
//            } else if (listaentrada.get(indicefilas).getInscrito() == 1) {
//                this.TInscripcion.setValueAt("Si", indicefilas, 5);
//            }
        }

        gradobo = new GradoBL();
        estudiantebo = new EstudianteBL();
        Vector vecgrados = new Vector(gradobo.consultarGrados());
        this.CBGrado.setModel(new DefaultComboBoxModel(vecgrados));
    }

    /** Creates new form InterfazInscripcion */
    public InterfazInscripcion() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.SOUTH);

        TableColumn column = null;
        /*Cambio el ancho de las columnas de la tabla TInscripcion*/
        /*column = this.jTable1.getColumnModel().getColumn(0);
        column.setPreferredWidth(85);
        column = this.jTable1.getColumnModel().getColumn(1);
        column.setPreferredWidth(100);
        column = this.jTable1.getColumnModel().getColumn(2);
        column.setPreferredWidth(100);
        column = this.jTable1.getColumnModel().getColumn(3);
        column.setPreferredWidth(120);
        column = this.jTable1.getColumnModel().getColumn(4);
        column.setPreferredWidth(120);
        column = this.jTable1.getColumnModel().getColumn(5);
        column.setPreferredWidth(70);*/

        estudiantebo = new EstudianteBL();
//        this.inicializarTablaDePreinscritos(estudiantebo.obtenerListadoPreinscritos());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        SPInscripcion = new javax.swing.JScrollPane();
        TInscripcion = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        BInscribir = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        BFiltrar = new javax.swing.JButton();
        CBGrado = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        BCancelar = new javax.swing.JButton();

        setTitle("Inscribir estudiantes");
        setResizable(false);

        TInscripcion.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Documento", "Apellido", "Nombre", "Fecha de inscripcion", "Grado al que aspira", "Inscrito"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TInscripcion.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        SPInscripcion.setViewportView(TInscripcion);
        TInscripcion.getColumnModel().getColumn(0).setResizable(false);
        TInscripcion.getColumnModel().getColumn(0).setPreferredWidth(170);
        TInscripcion.getColumnModel().getColumn(1).setResizable(false);
        TInscripcion.getColumnModel().getColumn(1).setPreferredWidth(300);
        TInscripcion.getColumnModel().getColumn(2).setResizable(false);
        TInscripcion.getColumnModel().getColumn(2).setPreferredWidth(80);

        jLabel1.setText("Marque con una \"S\" o con la palabra\"Si\"  los estudiantes que se inscribiran:");

        BInscribir.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BInscribir.setText("Inscribir Estudiantes");
        BInscribir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BInscribirActionPerformed(evt);
            }
        });

        jLabel3.setText("Tenga cuidado!!! Los cambios realizados aqui no se podrán modificar luego.");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "DATOS DE BUSQUEDA", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11), new java.awt.Color(0, 0, 255))); // NOI18N

        BFiltrar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BFiltrar.setText("Filtrar busqueda");
        BFiltrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BFiltrarActionPerformed(evt);
            }
        });

        CBGrado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("Seleccione el grado al que aspiran los estudiantes");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addComponent(CBGrado, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 122, Short.MAX_VALUE)
                .addComponent(BFiltrar, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BFiltrar)
                    .addComponent(jLabel4)
                    .addComponent(CBGrado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        BCancelar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BCancelar.setText("Cancelar");
        BCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BInscribir, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 355, Short.MAX_VALUE)
                                .addComponent(BCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(SPInscripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 750, Short.MAX_VALUE))
                            .addComponent(jLabel1)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(141, 141, 141)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(SPInscripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BInscribir)
                    .addComponent(BCancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BInscribirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BInscribirActionPerformed

        String temp = null;
        for (int i = 0; i < this.TInscripcion.getRowCount(); i++) {
            this.TInscripcion.setValueAt(String.valueOf(this.TInscripcion.getValueAt(i, 5)).toLowerCase(), i, 5); //conversion a minusculas
        }
//        int nummatriculados = estudiantebo.actualizarAInscritos(TInscripcion);
//        JOptionPane.showMessageDialog(this,
////                "Se han matriculado " + nummatriculados
////                + " Estudiantes",
//                "Operacion exitosa",
//                JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_BInscribirActionPerformed

    private void BFiltrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BFiltrarActionPerformed
        String idgrado = new String(this.CBGrado.getSelectedItem().toString());
//        if (!estudiantebo.encontrarEstudiantesPorGradoPreinscritos(idgrado).isEmpty()) {
//            this.inicializarTablaDePreinscritos((List<Estudiante>) estudiantebo.encontrarEstudiantesPorGradoPreinscritos(idgrado));
//        } else {
//            JOptionPane.showMessageDialog(this,
//                    "No se han encontrado estudiantes preinscritos que aspiren al grado:\n\n"
//                    + idgrado,
//                    "Error",
//                    JOptionPane.OK_OPTION);
//        }

    }//GEN-LAST:event_BFiltrarActionPerformed

    private void BCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_BCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazInscripcion().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCancelar;
    private javax.swing.JButton BFiltrar;
    private javax.swing.JButton BInscribir;
    private javax.swing.JComboBox CBGrado;
    private javax.swing.JScrollPane SPInscripcion;
    private javax.swing.JTable TInscripcion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    private class DefaultTableModelImpl extends DefaultTableModel {

        public DefaultTableModelImpl(Object[][] arg0, Object[] arg1) {
            super(arg0, arg1);
        }
        boolean[] canEdit = new boolean[]{false, false, false, false, false, true};

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit[columnIndex];
        }
    }
}
